// packages/group/pages/fans/index.js
import { ManageTargetInfoUpdateMixins, ShareImageMixins, PageScrollListMixins } from '../../../behaviors/page';
import { showLoading, hideLoading } from '../../../common/env';
import { getFansList, updateFansAlias } from '../../../api/brd';

Page({
    mixins: [ManageTargetInfoUpdateMixins, ShareImageMixins, PageScrollListMixins],
    data: {
        fansId: '',
        moreActionSheet: {
            show: false,
            list: [
                {
                    value: 'remark',
                    name: '备注',
                },
            ],
        },
        remarkModal: {
            show: false,
        },
    },
    onLoad(options) {
    },
    async _handleGetList(pageNo) {
        !this.data.loaded && showLoading('加载中...');
        const { pageData, filter, keyword } = this.data;
        let { ts } = pageData;
        pageNo === 1 && (ts = Date.now());
        const { rows = [], total } = await getFansList({ pageNo, ts, keyword, ...filter });
        this.setData({
            pageData: {
                ...pageData,
                refresh: false,
                ts,
                pageNo,
                total,
                rows: pageNo == 1 ? rows : [...pageData.rows, ...rows],
            },
        });
        if (!this.data.loaded) hideLoading(); this.setData({ loaded: true });
    },
    handleUserMore(e) {
        this.setData({
            'moreActionSheet.show': true,
            'fansId': e.detail,
        });
    },
    handleCloseMoreActionSheet() {
        this.setData({
            'moreActionSheet.show': false,
        });
    },
    handleSelectMoreActionSheet(e) {
        const { value } = e.detail;
        if (value === 'remark') {
            this.setData({
                'remarkModal.show': true,
            });
        }
        this.handleCloseMoreActionSheet();
    },
    async handleRemark(e) {
        const aliasName = e.detail;
        const { fansId } = this.data;
        if (!aliasName) return;
        const flag = await updateFansAlias({ fansId, aliasName });
        if (flag) this._handleGetList(1);
    },
})
